package com.ldzy.controller;

import com.alibaba.fastjson.JSONObject;
import com.ldzy.pojo.Cake;
import com.ldzy.pojo.Order;
import com.ldzy.pojo.Pay;
import com.ldzy.pojo.Shopping;
import com.ldzy.service.ICakeService;
import com.ldzy.service.IOrderService;
import com.ldzy.service.IPayService;
import com.ldzy.service.IShoppingService;
import com.ldzy.utilts.RespUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Map;

@RestController
@RequestMapping("/Order")
public class OrderController {
    @Resource
    IOrderService iOrderService;
    @Resource
    IShoppingService iShoppingService;
    @Resource
    ICakeService iCakeService;
    @Resource
    IPayService iPayService;

    @PostMapping("/addOrder")
    public RespUtil addOrder(@RequestBody Map<Object,Object> ids){
        Integer integer[];
        Long userId=null;
        double money=0.0;
        //自动生成订单id
        Calendar c=Calendar.getInstance();
        String time=new SimpleDateFormat("yyyy-MM-ddHHmmss").format(c.getTime()).toString();
        StringBuffer s=new StringBuffer(time.substring(14, 16));
        Long sys=System.currentTimeMillis();
        s.append(sys.toString().substring(11, 13));
        Double tm=Math.random()*10000+1;
        s.append(tm.toString().substring(tm.toString().length()-11, tm.toString().length()));
        System.out.println(s);
        //将map集合转换为字符串
        String idStr= ids.get("ids").toString();
        //分割字符串，只保留第一个到倒数第二个的值
       idStr =idStr.substring(1, idStr.length()-1);
       //根据, 以字符串数组进行分割字符串,并且保存在数据里面
        String[] split = idStr.split(", ");
        //进行数组初始化，数组长度为字符串数组的长度
        integer=new Integer[split.length];
        //遍历字符串数组并且以次转Integer类型并且保存在Integer数组里面
        for (int i = 0; i <split.length ; i++) {
            integer[i]=Integer.parseInt(split[i]);
            System.out.println(integer[i]);
        }

        for (int i = 0; i <integer.length ; i++) {
            Shopping shopping =new Shopping();
            Cake cake =new Cake();
            Order order =new Order();
            Long id =integer[i].longValue();
            //获取购物车信息
            shopping.setId(id);
            shopping=iShoppingService.shopping(shopping);
            System.out.println(shopping);
            //获取用户id
            if(i==0){
                userId= shopping.getUserId();
            }
            //获取商品信息
            cake.setId(shopping.getCakeId());
            cake=iCakeService.getById(cake.getId());
            System.out.println(cake);
            //跟据商品价格和购物车商品数量来计算金额
            money=money+ shopping.getNumber()*cake.getCakeMoney();
            //添加至订单表
            order.setNumber(shopping.getNumber());
            order.setUserId(shopping.getUserId());
            order.setOrderId(s+"");
            order.setCakeId(shopping.getCakeId());
            order.setMoney(cake.getCakeMoney());
            if (iOrderService.save(order)){
                iShoppingService.removeById(shopping.getId());
            }

        }
        Pay pay =new Pay();
        pay.setTime(LocalDateTime.now());
        pay.setOrderId(s+"");
        pay.setType("未支付");
        pay.setMoney(money);
        pay.setUserId(userId);
//return null;
        return  iPayService.insertPay(pay);
    }
}
